package com.ht.web_vr_home_tour_backend.config;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.ht.web_vr_home_tour_backend.obj.enums.ObjType;

import java.io.IOException;

public class ObjTypeDeSerialize extends JsonDeserializer<ObjType> {
    @Override
    public ObjType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
        TreeNode node = p.getCodec().readTree(p);
        TreeNode idNode = node.get("id");
        if (idNode.isMissingNode()) {
            throw new IllegalArgumentException("数据错误，请检查后重试");
        }
        if (idNode.isValueNode()) {
            String id = idNode.toString().replaceAll("\"", "");
            for (ObjType objType : ObjType.values()) {
                if (objType.name().equals(id)) {
                    return objType;
                }
            }
        }
        throw new IllegalArgumentException("未知数据类型");
    }
}
